<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class InvoicelistController extends Controller{
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
            array(
                'application.filters.RequireLoginFilter',
            ),
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'users' => array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }
    public function actionIndex($type){
        if($type!=Invoice::TYPE_CREDIT_MEMO && $type!=Invoice::TYPE_INVOICE && $type!=Invoice::TYPE_QUOTE && $type!=Invoice::TYPE_SALES_ORDERS){
            throw new CHttpException(404,'Request error');
        }
        Yii::app()->clientScript->registerScript('url',"var type='$type'",  CClientScript::POS_HEAD);
        $this->render('index',array(
            
        ));
    }
 
    
    public function actionListAllInvoice($type,$query=NULL,$page=0,$sort=NULL){
        if($page>0) $page--;
        $order=CJSON::decode($sort);
        $crit=new CDbCriteria();
        $crit->alias='i';
        if($query!=NULL){
            $crit->compare('i.custno', $query, false, 'or');
            $crit->compare('i.locno',$query,false,'or');
            $crit->compare('i.invoice',$query,false,'or');
            $crit->compare('i.salesordersorg',$query,false,'or');
            $crit->compare('i.quoteorg',$query,false,'or');            
            $crit->join='left join tbl_locations l on l.locno = i.locno AND l.custno=i.custno';
            $crit->compare('l.locname',$query,true,'or');
            $crit->compare('l.city',$query,false,'or');
            $crit->compare('l.state',$query,false,'or');
            $crit->compare('l.zip',$query,false,'or');
        }
        $crit->compare('i.invoicetype', $type,false,'and');
        if($sort!=NULL){
            $crit->order = $order[0]['property'].' '.$order[0]['direction'];
        }else{
            $crit->order='CAST( SUBSTR( invoice, 1,LOCATE(  "-", invoice )-1  ) AS SIGNED) ,CAST( SUBSTR( invoice, LOCATE(  "-", invoice ) +1 ) AS SIGNED )';
        }
        $invoiceList = new CActiveDataProvider('Invoice', array(
                    'criteria' => $crit,
                    'pagination' => array(
                        'pageSize' => 20,
                        'currentPage' => $page,
                    ),
                ));

        $invoiceArr = array();
        if ($invoiceList->getTotalItemCount() > 0) {
            $invoiceArr['totalCount'] = $invoiceList->getTotalItemCount();
           // $invoiceArr['type'] = 'Items';
            foreach ($invoiceList->getData() as $i => $item) {
                $invoiceArr['data'][] = $item->getAttributes();
                if($item->location!=NULL){
                    $invoiceArr['data'][$i]['locname']=$item->location->locname;
                }
                
                switch($type){
                    case Invoice::TYPE_QUOTE:
                        $invoiceArr['data'][$i]['quotestatustext']=$item->getQuoteStatusText();
                        break;
                    case Invoice::TYPE_SALES_ORDERS:
                        $invoiceArr['data'][$i]['salesorderstatustext']=JobsStatus::getText($item->salesorderstatus);
                        $invoiceArr['data'][$i]['hasinvoice']=$item->hasInvoice()?'Yes':'No';
                        break;
                    case Invoice::TYPE_INVOICE:
                        $invoiceArr['data'][$i]['amountowned']=$item->getAmountOwned();
                        if($invoiceArr['data'][$i]['amountowned']<0){
                            $invoiceArr['data'][$i]['amountowned']=0;
                        }
                        break;
                }
            }
            echo json_encode($invoiceArr);
        }
        
    }
}
?>
